package com.example.customviewtest.vertical_viewpager

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager.widget.PagerAdapter
import com.example.customviewtest.R

class VerticalViewPagerActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_vertical_view_pager)
        val vp = findViewById(R.id.vretical_vp) as VerticalViewPager
        val list = ArrayList<View>()
        val v1 = LayoutInflater.from(this).inflate(R.layout.viewpager_item_layout, null)
        v1.setBackgroundColor(Color.GRAY)
        val v2 = LayoutInflater.from(this).inflate(R.layout.viewpager_item_layout, null)
        v2.setBackgroundColor(Color.RED)
        val v3 = LayoutInflater.from(this).inflate(R.layout.viewpager_item_layout, null)
        v3.setBackgroundColor(Color.BLUE)
        val v4 = LayoutInflater.from(this).inflate(R.layout.viewpager_item_layout, null)
        v4.setBackgroundColor(Color.YELLOW)
        val v5 = LayoutInflater.from(this).inflate(R.layout.viewpager_item_layout, null)
        v5.setBackgroundColor(Color.GREEN)

        list.add(v1)
        list.add(v2)
        list.add(v3)
        list.add(v4)
        list.add(v5)
        vp.setPageTransformer(false, VerticalPageTransformer())
        vp.adapter = MyAdapter(list)
    }

    inner class MyAdapter(data: ArrayList<View>) : PagerAdapter() {
        override fun isViewFromObject(view: View, `object`: Any): Boolean {
            return view == `object`
        }

        override fun getCount(): Int {
            return list.size
        }

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            container.addView(list.get(position))
            return list.get(position)
        }

        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
            container.removeView(list.get(position))
        }

        lateinit var list: ArrayList<View>

        init {
            list = data
        }

    }
}